<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const data = [
            {
                id: 1,
                name: '冰箱',
                goods:[
                    {
                        id: 11,
                        name: 'Haier',
                    },
                    {
                        id: 12,
                        name: '西门子',
                    }
                ]
            },
            {
                id: 2,
                name: '电视',
                goods:[
                    {
                        id: 22,
                        name: 'changhong',
                    },
                    {
                        id: 23,
                        name: '索尼',
                    }
                ]
            },
            {
                id: 3,
                name: '洗衣机',
            },
        ]

        // 先横着找，再去第一家的孩子里面找

        // function findGoods (id, data) {
        //     const goods = data.find((item) => item.id === id);
        //     if (!goods) {
        //         for (const item of data) {
        //             const goods = findGoods(id, item.goods || []);
        //             if (goods) {
        //                 return goods;
        //             }
        //         }
        //     } else {
        //         // 找到了直接返回
        //         return goods;
        //     }
        // }
        // console.log(findGoods(2,data));
        // console.log(findGoods(22,data));


        function findGoods (id, data) {
            for (const item of data) {
                if (item.id === id) {
                    return item;
                }
                const goods = findGoods(id, item.goods || []);
                if (goods) {
                    return goods;
                }
            }
        }
        console.log(findGoods(12,data));
    </script>
</body>
</html>